package com.quanyan.runningriding.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.runningriding.entity.db.TbRunUpdatedVersion;
import com.quanyan.runningriding.entity.db.TbRunUpdatedVersionExample;
import com.quanyan.runningriding.entity.vo.run.resp.RespVersionVo;
import com.quanyan.runningriding.mapper.TbRunUpdatedVersionMapper;
import com.quanyan.runningriding.service.RunUpdatedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by liubin on 2016/10/31 0031.
 */
@Service
public class RunUpdatedServiceImpl implements RunUpdatedService {
    @Autowired
    private TbRunUpdatedVersionMapper tbRunUpdatedVersionMapper;

    @Override
    public APIResponse<RespVersionVo> updatedVersion(Byte platform,String appVersion){
        RespVersionVo respVersionVo=new RespVersionVo();
        TbRunUpdatedVersionExample tbRunUpdatedVersionExample=new TbRunUpdatedVersionExample();
        tbRunUpdatedVersionExample.createCriteria().andPlatformEqualTo(platform);
       List<TbRunUpdatedVersion> tbRunUpdatedVersions = tbRunUpdatedVersionMapper.selectByExample(tbRunUpdatedVersionExample);
        if (tbRunUpdatedVersions != null && tbRunUpdatedVersions.size() >= 0) {
            TbRunUpdatedVersion tbRunUpdatedVersion = tbRunUpdatedVersions.get(0);
            String appVersion2 = tbRunUpdatedVersion.getAppVersion();
            if (appVersion2.equals(appVersion)) {
                respVersionVo.setType((byte)1);
                respVersionVo.setAppVersion(tbRunUpdatedVersion.getAppVersion());
                respVersionVo.setPlatform(tbRunUpdatedVersion.getPlatform());
                return APIResponse.returnSuccess(respVersionVo);
            } else {
                respVersionVo.setType((byte)2);
                respVersionVo.setAppVersion(tbRunUpdatedVersion.getAppVersion());
                respVersionVo.setPlatform(tbRunUpdatedVersion.getPlatform());
                return APIResponse.returnSuccess(respVersionVo);
            }
        }
        return APIResponse.returnFail("服务器异常");
    }

}
